﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.AdminCustomer
{
    public partial class Customer : BaseForm
    {
        public Aliyeye.Car.Net.UI.CustomerSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CustomerSvc.SoapHeaderClass();

        private bool isDriver = false;
        private string strSearch = " 1=1";

        public Customer()
        {
            InitializeComponent();
        }
        public Customer(bool isDri)
        {
            InitializeComponent();
            isDriver = isDri;
        }
        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = strSearch;
            PageData pageData = new PageData(this, pager1, overlay);
            pageData.TableName = "Customer";
            pageData.PrimaryKey = "Id";
            pageData.OrderStr = "Id desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = "Id,Custype,Code,Name,Sex,Phone,Tel,CodeCar,CusCartype,ComName";

            DataSet ds = pageData.QueryDataTable();
            dgvInfo.AutoGenerateColumns = false;
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }

        private void Customer_Load(object sender, EventArgs e)
        {
            cbbDepart.SelectedIndex = 0;
            this.pager1.PageCurrent = 1;
            this.pager1.Bind();
        }
        //搜索
        private void btnSearch_Click(object sender, EventArgs e)
        {
            strSearch = " 1=1";
            if (!string.IsNullOrEmpty(txtUserName.Text))
            {
                strSearch += " And Name like '%" + txtUserName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtCode.Text))
            {
                strSearch += " And Code='" + txtCode.Text + "'";
            }
            if (!string.IsNullOrEmpty(cbbDepart.Text))
            {
                if (cbbDepart.Text == "个体户客户")
                {
                    strSearch += " And Custype='1'";
                }
                if (cbbDepart.Text == "企业客户")
                {
                    strSearch += " And Custype='2'";
                }
            }
            this.pager1.Bind();
        }
        //全部
        private void btnAll_Click(object sender, EventArgs e)
        {
            strSearch = " 1=1";
            this.pager1.Bind();
        }

        private int pager1_EventPaging(Aliyeye.Car.Net.UI.Components.EventPagingArg e)
        {
            return dgvBind();
        }

        private void dgvInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "客户类型")
            {
                if (e.Value != null)
                {
                    if (e.Value.ToString().Trim() == "1")
                    {
                        e.Value = "个体户客户";
                        //pernum = (Convert.ToInt32(this.dgvInfo.Rows[e.RowIndex].Cells["编号"].Value) + 6000);
                        //this.dgvInfo.Rows[e.RowIndex].Cells["客户编号"].Value = pernum;
                    }
                    else
                    {
                        e.Value = "企业客户";
                        //comnum = (Convert.ToInt32(this.dgvInfo.Rows[e.RowIndex].Cells["编号"].Value) + 1000);
                        //this.dgvInfo.Rows[e.RowIndex].Cells["客户编号"].Value = comnum;
                    }
                }
            }
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }
        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            String id = dgvInfo.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            String usertype = dgvInfo.Rows[e.RowIndex].Cells["客户类型"].Value.ToString();
            if (strButtonContent.Trim().Equals("修改"))
            {
                if (usertype == "2")
                {
                    CustomerCom userForm = new CustomerCom(isDriver, Convert.ToInt32(id));
                    userForm.ShowDialog();
                }
                else
                {
                    CustomerPer userForm = new CustomerPer(isDriver, Convert.ToInt32(id));
                    userForm.ShowDialog();
                }
            }
            else if (strButtonContent.Trim().Equals("删除"))
            {
                //"确定删除？"
                DialogConfirm diaSecc = new DialogConfirm("确定删除此客户信息吗？");
                if (diaSecc.ShowDialog() == DialogResult.OK)
                {
                    Model.Customer model = new Model.Customer();
                    model.Id = Convert.ToInt32(id);
                    //BLL.Customer bll = new BLL.Customer();
                    //bll.Delete(model.Id);
                    //txtCode.Text = "";
                    //txtUserName.Text = "";
                    //DialogNotify notify = new DialogNotify("删除客户信息成功！");
                    //notify.ShowDialog();


                    ServiceManipulator.SafeAsyncExecution<CustomerSvc.CustomerServiceSoapClient>(
                    proxy => proxy.Delete(sopaHeader, model.Id),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("删除客户信息成功！");
                            txtCode.Text = "";
                            txtUserName.Text = "";
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                    );
                }
            }
            else if (strButtonContent.Trim().Equals("查看"))
            {
                if (usertype == "2")
                {
                    CustomerCom userForm = new CustomerCom(isDriver, Convert.ToInt32(id), true);
                    userForm.ShowDialog();
                }
                else
                {
                    CustomerPer userForm = new CustomerPer(isDriver, Convert.ToInt32(id), true);
                    userForm.ShowDialog();
                }
            }
            this.pager1.Bind();
        }
        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

    }
}
